package com.example.wheelguide;

import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class BarriereActivity extends Activity{
	Button button1;
	RadioButton rb;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.barriere);
		
		Intent i = getIntent();
		final Barriere barriere = (Barriere) i.getSerializableExtra("barriere");
		
		
		rb = (RadioButton) findViewById(R.id.radioButton_kerbside_yes);
		
		if(barriere.getKerbside_()!=0)
		{
			rb.setChecked(true);
			System.out.println("Bordsteine ja");
			TextView tv = (TextView) findViewById(R.id.TextView_kerbside_height);
			tv.setVisibility(View.VISIBLE);
			EditText et = (EditText) findViewById(R.id.editText_kerbside_height);
			et.setVisibility(View.VISIBLE);
			et.setText(Integer.toString(barriere.getKerbside_()));
		}
		
		rb.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				TextView tv = (TextView) findViewById(R.id.TextView_kerbside_height);
				tv.setVisibility(View.VISIBLE);
				EditText et = (EditText) findViewById(R.id.editText_kerbside_height);
				et.setVisibility(View.VISIBLE);
				
				
			}
		});
		
		rb = (RadioButton) findViewById(R.id.radioButton_kerbside_no);
		rb.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				TextView tv = (TextView) findViewById(R.id.TextView_kerbside_height);
				tv.setVisibility(View.INVISIBLE);
				EditText et = (EditText) findViewById(R.id.editText_kerbside_height);
				et.setVisibility(View.INVISIBLE);
				
				
			}
		});
		
		rb = (RadioButton) findViewById(R.id.radioButton_pitch_yes);
		
		if(barriere.getPitch_()!=0)
		{
			rb.setChecked(true);
			TextView tv = (TextView) findViewById(R.id.textView_pitch_max);
			tv.setVisibility(View.VISIBLE);
			EditText et = (EditText) findViewById(R.id.EditText_pitch_max);
			et.setVisibility(View.VISIBLE);
			et.setText(Integer.toString(barriere.getPitch_()));
		}
		
		
		rb.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				TextView tv = (TextView) findViewById(R.id.textView_pitch_max);
				tv.setVisibility(View.VISIBLE);
				EditText et = (EditText) findViewById(R.id.EditText_pitch_max);
				et.setVisibility(View.VISIBLE);
				
				
			}
		});
		
		rb = (RadioButton) findViewById(R.id.radioButton_pitch_no);
		rb.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				TextView tv = (TextView) findViewById(R.id.textView_pitch_max);
				tv.setVisibility(View.INVISIBLE);
				EditText et = (EditText) findViewById(R.id.EditText_pitch_max);
				et.setVisibility(View.INVISIBLE);
				
				
			}
		});
		
		
		
		rb = (RadioButton) findViewById(R.id.radioButton_stairs_yes);
		
		if(barriere.getStairs_()!=0)
		{
			rb.setChecked(true);
			TextView tv = (TextView) findViewById(R.id.textView_stairs_max);
			tv.setVisibility(View.VISIBLE);
			EditText et = (EditText) findViewById(R.id.editText_stairs_max);
			et.setVisibility(View.VISIBLE);
			et.setText(Integer.toString(barriere.getStairs_()));
		}
		
		rb.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				TextView tv = (TextView) findViewById(R.id.textView_stairs_max);
				tv.setVisibility(View.VISIBLE);
				EditText et = (EditText) findViewById(R.id.editText_stairs_max);
				et.setVisibility(View.VISIBLE);
				
				
			}
		});
		
		rb = (RadioButton) findViewById(R.id.radioButton_stairs_no);
		rb.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				TextView tv = (TextView) findViewById(R.id.textView_stairs_max);
				tv.setVisibility(View.INVISIBLE);
				EditText et = (EditText) findViewById(R.id.editText_stairs_max);
				et.setVisibility(View.INVISIBLE);
				
				
			}
		});
		
		
		
		rb = (RadioButton) findViewById(R.id.radioButton_stairs_moving_yes);
		
		if(barriere.getStairs_moving_())
		{
			rb.setChecked(true);
		}
			
				
		button1 = (Button) findViewById(R.id.Button_apply);	
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				EditText et;
				boolean alert=false;
				String text="";
				
				
				RadioGroup rg= (RadioGroup) findViewById(R.id.radioGroup_kerbside);
				if(rg.getCheckedRadioButtonId()==R.id.radioButton_kerbside_yes)
				{
					et = (EditText) findViewById(R.id.editText_kerbside_height);
					et.getText().toString();
					if(et.getText().toString().length()==0)
					{
						alert=true;
						text=text + getString(R.string.TextView_kerbside_height)+", ";
					}
					else
					{
						barriere.setKerbside_(Integer.parseInt(et.getText().toString()));
					}
				}
				else 
				{
					barriere.setKerbside_(0);
				}
				
				rg = (RadioGroup) findViewById(R.id.radioGroup_stairs);
				if(rg.getCheckedRadioButtonId()==R.id.radioButton_stairs_yes)
				{
					et = (EditText) findViewById(R.id.editText_stairs_max);
					et.getText().toString();
					if(et.getText().toString().length()==0)
					{
						alert=true;
						text=text + getString(R.string.TextView_stairs_max)+", ";
					}
					else
					{
						barriere.setStairs_(Integer.parseInt(et.getText().toString()));
					}
				}
				else
				{
					barriere.setStairs_(0);
				}
				
				rg = (RadioGroup) findViewById(R.id.radioGroup_stairs_moving);
				if(rg.getCheckedRadioButtonId()==R.id.radioButton_stairs_moving_yes)
				{
					barriere.setStairs_moving_(true);
				}
				else
				{
					barriere.setStairs_moving_(false);	
				}
				
				
				rg = (RadioGroup) findViewById(R.id.radioGroup_pitch);
				if(rg.getCheckedRadioButtonId()== R.id.radioButton_pitch_yes)
				{
					et = (EditText) findViewById(R.id.EditText_pitch_max);
					et.getText().toString();
					if(et.getText().toString().length()==0)
					{
						alert=true;
						text=text + getString(R.string.TextView_pitch_max)+", ";
					}
					else
					{
						barriere.setPitch_(Integer.parseInt(et.getText().toString()));
					}
				}
				else
				{
					barriere.setPitch_(0);
				}
				
				
				if(alert)
				{
					AlertDialog alertDialog= new AlertDialog.Builder(v.getContext()).create();
					alertDialog.setTitle("!!!Angaben fehlen!!!");
					alertDialog.setMessage("Bitte tragen Sie "+ text + "nach.");
					alertDialog.setButton(1,"OK", new DialogInterface.OnClickListener() {
					public void onClick(final DialogInterface dialog, final int which) {
						dialog.cancel(); 
					}
					});
					alertDialog.show();
					
				}
				else
				{
	
					Intent i = new Intent();
					Bundle b= new Bundle();
					b.putSerializable("return_barriere", barriere);
					i.putExtras(b);
					setResult(101, i);
					finish();
				}
				
				
				
			}
		});
		
		button1 = (Button) findViewById(R.id.button_back);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
